package com.example.smartcity_40.house;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;


import com.example.smartcity_40.R;
import com.example.smartcity_40.house.apter.HoustlistApter;
import com.example.smartcity_40.house.bean.HoustlistBean;
import com.example.smartcity_40.tool.Tool;

import java.util.ArrayList;
import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class HouseActivity extends AppCompatActivity {

    private HouseActivity context;
    private Tool tool;
    private HoustlistApter houstlistApter;
    private RecyclerView houselist;
    private SearchView sear;
    private String TAG = "TAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house);
        context = this;
        initview();
        houstlistmethod("");
    }

    private void houstlistmethod(String houseType) {
        String url = "/prod-api/api/house/housing/list?pageNum=1&pageSize=10";
        if(!TextUtils.isEmpty(houseType.trim())){
            url = url +"&houseType="+houseType;
        }
        tool.send(url,
                Tool.GET,
                null,
                true,
                HoustlistBean.class,
                new Function1<HoustlistBean, Unit>() {
                    @Override
                    public Unit invoke(HoustlistBean houstlistBean) {
                        if(houstlistBean.getCode()==200){
                            houselist.setLayoutManager(new LinearLayoutManager(context));
                            houstlistApter.setDate(houstlistBean.getRows());
                            houselist.setAdapter(houstlistApter);
                            searmethod(houstlistBean.getRows());
                        }
                        return null;
                    }
                });
    }

    private void searmethod(List<HoustlistBean.RowsBean> rows) {
        sear.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //点击搜索触发
                ArrayList<HoustlistBean.RowsBean> rowsBeans = new ArrayList<>();
                for (HoustlistBean.RowsBean row : rows) {
                    if (row.getSourceName().toLowerCase().contains(query.toLowerCase())) {
                        rowsBeans.add(row);
                    }
                }
                houstlistApter.setDate(rowsBeans);
                houselist.setAdapter(houstlistApter);
                houstlistApter.notifyDataSetChanged();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //点击输入的内容和点击退出
                return false;
            }
        });
    }

    private void initview() {
        ImageView back = findViewById(R.id.back);
        FrameLayout second_hand = findViewById(R.id.second_hand);
        FrameLayout Renting = findViewById(R.id.Renting);
        FrameLayout properties = findViewById(R.id.properties);
        FrameLayout intermediary = findViewById(R.id.intermediary);
        sear = findViewById(R.id.sear);
        houselist = findViewById(R.id.houselist);
        tool = new Tool(context);
        houstlistApter = new HoustlistApter(context);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });



        second_hand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                houstlistmethod("二手");
            }
        });


        Renting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                houstlistmethod("租房");
            }
        });


        properties.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                houstlistmethod("楼盘");
            }
        });


        intermediary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                houstlistmethod("中介");
            }
        });

    }
}